<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\MateServices;
use App\Http\Services\UserServices;

class ApiMateController extends BaseController
{
    protected $mateServices;
    protected $userServices;


    public function __construct(){
        $this->mateServices = new MateServices();
        $this->userServices = new UserServices();
    }

    /**
     * 缘分配对用户信息
     */
    public function userInfo(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        //获取用户今日剩余匹配次数
        $num = $this->mateServices->getUserMateNum($userInfo['user_id']);

        $result = [];
        $result['user_id'] = $userInfo['user_id'];
        $result['gender'] = $userInfo['gender'];
        $result['num'] = $num;

        Response::returnResult(Hint::SUCCESS, $result);
    }

    /**
     * 钻石兑换缘分配对次数
     */
    public function buyMateNum(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $data = $this->mateServices->buyMateNum($userInfo['user_id']);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 消耗次数获取情话
     */
    public function getLoveTalk(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $data = $this->mateServices->getLoveTalk($userInfo['user_id']);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 获取匹配主播
     */
    public function getLoveUser(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $data = $this->mateServices->getLoveUser($userInfo['user_id']);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 获取语音通话配置
     */
    public function voiceCallConfig(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $data = $this->mateServices->voiceCallConfig($userInfo['user_id']);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 支付语音通话费用
     */
    public function payVoiceCall(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $callUserId = $request->post('call_user_id');
        if(empty($callUserId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->mateServices->payVoiceCall($userInfo['user_id'], $callUserId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

}
